<#import "../common/layout.ftl" as layout/>
<#import "../common/dataTables.ftl" as dataTables/>
<#import "../common/modern-forms.ftl" as mform/>
<#import "../common/modal.ftl" as modal/>


<!DOCTYPE html>
<html>
<@layout.head title="资产移交">
    <@dataTables.head/>
    <@dataTables.buttons_head/>
    <@dataTables.select_head/>
    <@dataTables.fixedHeader_head/>
    <@mform.head/>
    <@select.head/>
</@layout.head>
<body>
<@layout.page>
<div class="main-box">
    <div class="content">
        <div style="position: relative;top:50px; display: flex">
            <div style="width: 500px; position: relative">

                <div>
                    <div style="position: absolute;top: 0px">
                        <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                                onclick="javascript:refreshData();">
                            <i class="fa fa-search"></i> 查询
                        </button>
                        <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                                onclick="javascript:newdetail();">
                            <i class="fa fa-plus"></i> 新增
                        </button>
                        <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                                onclick="javascript:editdetail();">
                            <i class="fa fa-edit"></i> 修改
                        </button>
                        <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                                onclick="javascript:deldetail();">
                            <i class="fa fa-minus"></i> 删除
                        </button>
                    </div>
                </div>
                <@dataTables.commonTable id="mainTable1"/>
            </div>
            <div style="flex: 1;margin-left:20px; position: relative">
                <div>
                    <div style="position: absolute;top: 0px">
                        <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                                onclick="javascript:print();">
                            <i class="fa fa-print"></i> 打印变动明细
                        </button>
                        <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                                onclick="javascript:editEchange();">
                            <i class="fa fa-edit"></i> 修改变动明细
                        </button>
                        <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                                onclick="javascript:deldetail2();">
                            <i class="fa fa-minus"></i> 删除变动明细
                        </button>
                        <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                                onclick="javascript:changeStatedetail2();">
                            <i class="fa fa-check"></i> 变动生效
                        </button>
                    </div>
                </div>
                <@dataTables.commonTable id="mainTable2"/>
            </div>
        </div>
        <div id="searchGroup" class="btn-group"
             style="position: absolute;top:10px;display: flex;width:calc(100% - 15px);">
            <div style=" width: 500px;" id="orgBox">

            </div>
        </div>
    </div>

    <!-- 弹出窗 -->
    <@modal.editModal id="modal-detail" buttonId="btndetailSave" width="800px" height="100px">
        <@mform.container  id="editForm">
            <input id="id" name="id" type="hidden"/>
            <@mform.row>
                <@mform.select2 id='staffId' name='staffId' label="使用人" onchange="staffChange()"  size=6>
                    <option value="">请选择使用人</option>
                    <#list staffList as item>
                        <option value="${item.id?c}">${item.name}</option>
                    </#list>
                </@mform.select2>
                <@mform.select2 id='typeU' name='type' label="变化方式"  size=6>
                    <#list changeType as item>
                        <option value="${item.code}">${item.name}</option>
                    </#list>
                </@mform.select2>
            </@mform.row>
            <@mform.row>
                <@mform.input_text id='oldType' name='oldType' size=6 label='原人员类型' icon='fa-lock' placeholder="未设置"  readonly="true"/>
                <@mform.select2 id='newType' name='newType' label="新人员类型"  size=6>
                    <#list staffType as item>
                        <option value="${item.code}">${item.name}</option>
                    </#list>
                </@mform.select2>
            </@mform.row>
            <@mform.row>
                <@mform.input_text id='oldOrg' name='oldOrg' size=6 label='原所属机构' icon='fa-lock' placeholder="未设置"  readonly="true"/>
                <@mform.select2 id='newOrgU' name='newOrg' label="新所属机构"  size=6>
                </@mform.select2>
            </@mform.row>
            <@mform.row>
                <@mform.input_text id='oldRole' name='oldRole' size=6 label='原角色' icon='fa-lock' placeholder="未设置"  readonly="true"/>
                <@mform.select2 id='newRole' name='newRole' label="新角色"  size=6>
                    <#list staffRole as item>
                        <option value="${item.cRoleID}">${item.cRoleName}</option>
                    </#list>
                </@mform.select2>
            </@mform.row>
        </@mform.container>
    </@modal.editModal>

    <!-- 弹出窗 -->
    <@modal.editModal id="modal-detail2" buttonId="btndetailSave2" width="800px" height="100px">
        <@mform.container  id="editForm2">
            <input id="fId" name="fId" type="hidden"/>
            <input id="id" name="id" type="hidden"/>
            <input id="eId" name="eId" type="hidden"/>
            <@mform.row>
                <@mform.select2 id='type' name='type' label="变化方式"  size=6 onchange="eChangeTypeChange()">
                    <#list echangeType as item>
                        <option value="${item.code}">${item.name}</option>
                    </#list>
                </@mform.select2>
            </@mform.row>
            <div id="warehouseBox">
                <@mform.row>
                    <@mform.select2 id='warehouseId' name='warehouseId' label="选择仓库"  size=12>
                        <#list warehouseList as item>
                            <option value="${item.id?c}">${item.name}</option>
                        </#list>
                    </@mform.select2>
                </@mform.row>
            </div>
            <div id="userBox">
                <@mform.row>
                    <@mform.input_text id='oldOrg' name='oldOrg' size=6 label='原所属机构' icon='fa-lock' placeholder="未设置"  readonly="true"/>
                    <@mform.select2 id='newOrg' name='newOrg' label="新所属机构"  size=6>
                    </@mform.select2>
                </@mform.row>
                <@mform.row>
                    <@mform.input_text id='oldStaff' name='oldStaff' size=6 label='原使用人' icon='fa-lock' placeholder="未设置"  readonly="true"/>
                    <@mform.select2 id='newStaff' name='newStaff' label="新使用人"  size=6>
                        <#list staffList as item>
                            <option value="${item.id?c}">${item.name}</option>
                        </#list>
                    </@mform.select2>
                </@mform.row>
                <@mform.row>
                    <@mform.input_text id='oldUseType' name='oldUseType' size=6 label='原使用方式' icon='fa-lock' placeholder="未设置"  readonly="true"/>
                    <@mform.select2 id='newUseType' name='newUseType' label="新使用方式"  size=6>
                        <#list useType as item>
                            <option value="${item.code}">${item.name}</option>
                        </#list>
                    </@mform.select2>
                </@mform.row>
                <@mform.row>
                    <@mform.input_text id='oldPosition' name='oldPosition' size=6 label='原使用位置' icon='fa-lock' placeholder="未设置"  readonly="true"/>
                    <@mform.input_text id='newPosition' name='newPosition' size=6 label='新使用位置' icon='fa-edit' placeholder="请输入"  />
                </@mform.row>
            </div>
        </@mform.container>
    </@modal.editModal>
</@layout.page>
<@layout.foot>
    <@dataTables.foot/>
    <@dataTables.buttons_foot/>
    <@dataTables.select_foot/>
    <@dataTables.fixedHeader_foot/>
    <@mform.foot/>

    <script src="${request.contextPath}/assets/js/jquery.serializejson.min.js?v=3653060703"></script>
    <script type="text/javascript">
        var orgInfo =${organizationName};
        var orgList = [];
        var orgSelect = $("<select id='oneOrganization' style='width:100%;' onchange='refreshData()'><option value='0'>请选择机构</option></select>");
        $("#orgBox").append(orgSelect);
        var orgEditSelect = $("#editForm #newOrgU");
        var orgEditSelect2 = $("#editForm2 #newOrg");
        putOptions(orgSelect, "", orgInfo.list);
        putOptions(orgEditSelect2, "", orgInfo.list);
        putOptions(orgEditSelect, "", orgInfo.list);
        $("#editForm2 #type").find("[value='123001']").remove();
        $("#editForm2 #type").find("[value='123009']").remove();
        $("#editForm2 #userBox").hide();
        $("#editForm2 #warehouseBox").hide();

        function putOptions(orgSelect, supername, list) {
            if (supername != "") supername = supername + "-";
            for (var i = 0; i < list.length; i++) {
                orgSelect.append("<option value=" + list[i].id + ">" + supername + list[i].name + "</option>")
                if (list[i].childrenOrg.length > 0) {
                    putOptions(orgSelect, supername + list[i].name, list[i].childrenOrg);
                }
            }
        }

        function putList(orgList, supername, list) {
            if (supername != "") supername = supername + "-";
            for (var i = 0; i < list.length; i++) {
                orgList.push({
                    id: list[i].id,
                    name: list[i].name
                })
                if (list[i].childrenOrg.length > 0) {
                    putList(orgList, supername + list[i].name, list[i].childrenOrg);
                }
            }
        }

        putList(orgList, "", orgInfo.list);

        function getOrgName(id) {
            for (var i = 0; i < orgList.length; i++) {
                if (orgList[i].id == id) return orgList[i].name
            }
        }

        orgSelect.select2();


        function staffChange() {
            staffInfo = staffList[$("#editForm #staffId").val()];
            $("#editForm #oldType").val(staffTypeList[staffInfo.staffType]);
            $("#editForm #oldOrgU").val(getOrgName(staffInfo.organizationId));
            $("#editForm #oldRole").val(roleList[staffInfo.role]);
        }

        function eChangeTypeChange() {
            var type = $("#editForm2 #type").val();
            if ("123004,123005".indexOf(type) >= 0) {
                $("#editForm2 #warehouseBox").show();
                $("#editForm2 #userBox").hide();
            } else {
                $("#editForm2 #warehouseBox").hide();
                $("#editForm2 #userBox").show();
            }
        }

        var changeTypeList = {
            <#list changeType as item>
                "${item.code}": "${item.name}",
            </#list>}
        var echangeTypeList = {
            <#list echangeType as item>
                "${item.code}": "${item.name}",
            </#list>}
        var warehouseList = {
            <#list warehouseList as item>
                "${item.id?c}": "${item.name}",
            </#list>}
        var staffTypeList = {
            <#list staffType as item>
                "${item.code}": "${item.name}",
            </#list>}
        var useTypeList = {
            <#list useType as item>
                "${item.code}": "${item.name}",
            </#list>}
        var roleList = {
            <#list staffRole as item>
                "${item.cRoleID}": "${item.cRoleName}",
            </#list>}
        var state = {
            <#list state as item>
                "${item.code}": "${item.name}",
            </#list>}
        var staffList = {
            <#list staffList as item>
                "${item.id?c}": {
                    name: "${item.name}",
                    organizationId: "${item.organizationId?c}",
                    role: "${item.role}",
                    staffType: "${item.staffType}"
                },
            </#list>}
        var staffInfo;
        var mainTable1 = $('#mainTable1').winningTable({
            columns: [
                {"sWidth": "40px", "title": "变动方式", "data": "type"},
                {"sWidth": "80px", "title": "变动时间", "data": "createTime"},
                {"sWidth": "50px", "title": "人员姓名", "data": "name"},
                {"sWidth": "50px", "title": "认证号", "data": "telephone"}
            ],
            pageLength: 10,
            fixedHeader: false,
            searching: true,
            ordering: false,
            "rowCallback": function (row, data, index) {
                $('td', row).eq(0).html(changeTypeList[data.type]);
            }
        });
        var mainTable2 = $('#mainTable2').winningTable({
            columns: [
                {"sWidth": "100px", "title": "机构名称", "data": "organizationId"},
                {"sWidth": "120px", "title": "资产名称", "data": "name"},
                {"sWidth": "120px", "title": "资产条形码", "data": "barCode"},
                {"sWidth": "120px", "title": "二维码", "data": "QrCode"},
                {"sWidth": "80px", "title": "领用时间", "data": "buyTime"},
                {"sWidth": "80px", "title": "使用方式", "data": "useType"},

                {"sWidth": "100px", "title": "移交类型", "data": "type"},
                {"sWidth": "80px", "title": "新使用方式", "data": "newUseType"},
                {"sWidth": "100px", "title": "新机构", "data": "newOrg"},
                {"sWidth": "80px", "title": "新使用人", "data": "newStaff"},
                {"sWidth": "80px", "title": "新位置", "data": "newPosition"},
                {"sWidth": "80px", "title": "暂存仓库", "data": "warehouseId"},
                {"sWidth": "80px", "title": "变动状态", "data": "state"}

            ],
            pageLength: 10,
            searching: true,
            ordering: false,
            "rowCallback": function (row, data, index) {
                $('td', row).eq(0).html(getOrgName(data.oldOrg||data.organizationId));
                $('td', row).eq(5).html(useTypeList[data.oldUseType||data.useType]);
                $('td', row).eq(6).html(echangeTypeList[data.type]);
                $('td', row).eq(7).html(useTypeList[data.newUseType]);
                $('td', row).eq(8).html(getOrgName(data.newOrg));
                if (staffList[data.newStaff] != null) $('td', row).eq(9).html(staffList[data.newStaff]["name"]);
                $('td', row).eq(11).html(warehouseList[data.warehouseId]);
                $('td', row).eq(12).html(state[data.state]);
//                $('td', row).eq(7).html(secrecyMap[data.secrecy]);
            }
        });

        function newdetail() {
            $('#modal-detail').find(".modal-title").text('新增');
            clearForm($('#modal-detail'));
            staffInfo = null;
            $("#btndetailSave").unbind();
            $("#btndetailSave").click(savedetail);
            $('#modal-detail').modal();
        }

        function editdetail() {
            var datas = mainTable1.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var $modal = $("#modal-detail");
            $modal.find(".modal-title").text("修改信息");
            clearForm($modal);
            var data = datas[0];
            initFormItemData($("#editForm"), data);
            staffChange();
            $("#editForm #oldType").val(staffTypeList[data.oldType]);
            $("#editForm #oldRole").val(roleList[data.oldRole]);
            $("#editForm #oldOrgU").val(getOrgName(data.oldOrg));
            $modal.modal();
            $("#btndetailSave").unbind();
            $("#btndetailSave").click(savedetail);
        }

        function editEchange() {
            var datas = mainTable2.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            if ("334001".indexOf(datas[0].state)<0) {
                swal("错误", "当前状态不允许此操作！", "error");
                return;
            }
            var $modal = $("#modal-detail2");
            $modal.find(".modal-title").text("修改变动明细");
            clearForm($modal);
            var data = datas[0];
            initFormItemData($("#editForm2"), data);
            eChangeTypeChange();
            $("#editForm2 #fId").val(data.fId);
            $("#editForm2 #eId").val(data.eqId);
            $("#editForm2 #oldUseType").val(useTypeList[data.useType]);
            $("#editForm2 #oldStaff").val(staffList[data.staffId]["name"]);
            $("#editForm2 #oldOrg").val(getOrgName(data.organizationId));
            $modal.modal();
            $("#btndetailSave2").unbind();
            $("#btndetailSave2").click(saveEChangedetail);
        }

        function refreshData() {
            var data = {
                datas: JSON.stringify({
                    oneOrganization: $("#oneOrganization").val()
                })
            }
            mainTable1.queryData("${request.contextPath}/busi/equipment/useChange/queryUchangeList", data);
        }

        refreshData();

        function refreshData2() {
            var datas = mainTable1.getSelectedDatas();
            if (datas.length >= 0)
                mainTable2.queryData("${request.contextPath}/busi/equipment/useChange/queryList", {
                    datas: JSON.stringify({
                        fId: datas[0].id,
                        source:"124002",
                        useStaffId: datas[0].staffId
                    })
                });
            else
                mainTable2.cleanData();
        }

        $("#mainTable1").on("click", function () {
            refreshData2()
        })

        function savedetail() {
            if (staffInfo == null) {
                swal("错误", "请选择变动的人员", "error");
                return;
            }
            swal({
                title: "确定保存",
                text: "是否确定保存此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = $("#editForm form").serializeJSON();
                requestData.oldType = staffInfo.staffType;
                requestData.oldOrg = staffInfo.organizationId;
                requestData.oldRole = staffInfo.role;
                requestData.source ="124002";
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/busi/equipment/useChange/saveUChange',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                $("#modal-detail").modal('hide');
                                refreshData();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }

        function deldetail() {
            var datas = mainTable1.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var data = datas[0];
            swal({
                title: "确定删除",
                text: "是否确定删除此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = {
                    id: data.id
                };
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/busi/equipment/useChange/deleteU',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                refreshData();
                                refreshData2();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }
        function deldetail2() {
            var datas = mainTable2.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var data = datas[0];
            if ("334001".indexOf(datas[0].state)<0) {
                swal("错误", "当前状态不允许此操作！", "error");
                return;
            }
            swal({
                title: "确定删除",
                text: "是否确定删除此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = {
                    id: data.id
                };
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/busi/equipment/useChange/delete',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                refreshData2();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }
        function changeStatedetail2() {
            var datas = mainTable2.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            if ("334001".indexOf(datas[0].state)<0) {
                swal("错误", "当前状态不允许此操作！", "error");
                return;
            }
            var data = datas[0];
            swal({
                title: "确定生效",
                text: "是否确定此资产变动生效，生效后资产当前信息将发生修改且不可撤销！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = {
                    id: data.id,
                    state:"334002",
                    reason:""
                };
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/busi/equipment/useChange/checkEChange',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                refreshData2();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }
        function saveEChangedetail() {
            var datas = mainTable2.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var data = datas[0];
            swal({
                title: "确定保存",
                text: "是否确定保存此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = $("#editForm2 form").serializeJSON();
                requestData.oldUseType = data.useType;
                requestData.oldOrg = data.organizationId;
                requestData.oldStaff = data.staffId;
                requestData.source="124002";
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/busi/equipment/useChange/saveEChange',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                $("#modal-detail2").modal('hide');
                                refreshData2()
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }
        function print() {
            var datas = mainTable1.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var data = datas[0];
            window.open('${request.contextPath}/busi/equipment/useChange/print/'+data.id);
        }
    </script>
</@layout.foot>
</body>
</html>